if not menu then menu = {} end

if not menu.chart then

dofile (getScriptPath() .. "\\..\\lib\\lib.lua")
dofile (getScriptPath() .. "\\..\\lib\\orders.lua")

menu.chart = {changed = {}}

processor:AddInit(function()
	menu.chart.charts = lib:GetChartList("CHART")
	if #menu.chart.charts < 1 then
		message("No Chart Found")
		processor:stop(3)
		return
	end
end)

processor:AddInit(function()
	window:AddRow()
	menu.chart.row = window:AddRow()
	menu.chart.rows = 1
	local c, l
	local r, cols = window:size()
	
	for i, v in ipairs(menu.chart.charts) do
		menu.chart.charts[v] = {
			index = i,
			col = (i - 1) % cols + 1,
			row = (i - ((i - 1) % cols + 1)) / cols,
			changed = {},
			b_color = QTABLE_DEFAULT_COLOR,
			f_color = QTABLE_DEFAULT_COLOR
		}
		
		if i > 1 then
			c = (i - 1) % cols + 1
			if c == 1 then
				r = window:AddRow()
				menu.chart.rows = menu.chart.rows + 1
			end
		end
	end
end)

processor:AddInit(function()
	menu.chart:Select(1, 1)
	--menu.chart.index = 1
end)

processor:AddHandler("OnTableNotification", function(data)
	if data.msg == 11 and data.par1 >= menu.chart.row
		and data.par1 < menu.chart.row + menu.chart.rows
	then
		menu.chart:Select(data.par1 - menu.chart.row + 1, data.par2)
	end
end)

processor:AddTimer(function()
	for k, v in pairs(menu.chart.charts) do
		if type(v) == "table"
			and (not v.instrument
				or v.instrument.short_name ~= lib:GetChartInstrumentName(k)
			)
		then
			menu.chart:SetInstrument(v, lib:GetChartInstrument(k))
		end
	end
end)

processor:AddTimer(function()
	for k, chart in pairs(menu.chart.charts) do
		menu.chart:SetChartColor(chart, chart.instrument)
	end
end)

function menu.chart:SetChartColor(chart, instrument)
	if type(chart) == "table" and instrument then
		local color = QTABLE_DEFAULT_COLOR
		local pos, limits = orders:GetPosition(instrument)
		if pos > 0 then
			color = RGB(0, 128, 0)
		elseif pos < 0 then
			color = RGB(192, 0, 0)
		elseif limits ~= 0 then
			color = RGB(128, 0, 255)
		end
		
		if chart.f_color ~= color then
			chart.f_color = color
			window:SetColor(menu.chart.row + chart.row, chart.col, chart)
		end
	end
end

function menu.chart:Select(row, col)
	local index, cols = window:size()
	if col > 0 and col <= cols and row > 0 and row <= self.rows then
		index = (row - 1) * cols + col
		if index <= #self.charts then
			if self.index then		-- skip script run selection
				lib:SelectChartTab(index)
			end
			
			if self.index ~= index then
				if self.index then
					local c = (self.index - 1) % cols + 1
					local r = (self.index - c) / cols
					self.charts[ self.charts[self.index] ].b_color = QTABLE_DEFAULT_COLOR
					window:SetColor(self.row + r, c, self.charts[ self.charts[self.index] ])
				end
				
				self.index = index
				self.tag = self.charts[index]
			
				self.charts[ self.charts[index] ].b_color = RGB(192, 192, 255)
				window:SetColor(self.row + row - 1, col, self.charts[ self.charts[self.index] ])
				
				self:SetInstrument(self.charts[ self.charts[index] ])
			end
		end
	end
end

function menu.chart:SetInstrument(chart, instrument)
	if chart then
		local c
		local r, cols = window:size()
		
		if instrument then		-- changed on chart
			c = (chart.index - 1) % cols + 1
			r = (chart.index - c) / cols
			window:SetCell(self.row + r, c, instrument.code)
			if chart.instrument then chart.changed.instrument = true end
			chart.instrument = instrument
		end
			
		if chart.instrument and self.index == chart.index then
			self.instrument = chart.instrument
			
			self.changed = chart.changed
			chart.changed = {}
			self.changed.selected = true
		
			menu.window.caption = chart.instrument.short_name
		end
	end
end

end	-- of file
